package com.joker.online_exam.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.joker.userExam.dao.mapper.SubjectOneQuestionMapper;
import com.joker.userExam.model.SubjectFourQuestionTrunk;
import com.joker.userExam.dao.repo.SubjectFourQuestionTrunkRepo;
import com.joker.userExam.dao.repo.SubjectOneQuestionRepo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李杰
 * @date 2021/3/15 - 23:36
 */
@SpringBootTest
public class DataBaseInsert {
    @Autowired
    SubjectOneQuestionMapper subjectMapper;
    @Autowired
    SubjectOneQuestionRepo subjectOne;
    @Autowired
    SubjectFourQuestionTrunkRepo subjectFourTrunk;


    @Test
    void insertQuestionIntoDataBase() throws Exception {
        StringBuffer buffer = new StringBuffer();
        BufferedReader bf = new BufferedReader(new FileReader("C:/Users/29596/Desktop/bs/json/tSubject4.json"));
        String s = null;
        StringBuilder result = new StringBuilder();
        while ((s = bf.readLine()) != null) {//使用readLine方法，一次读一行
            result.append(s.trim());
        }
        System.err.println(result);
        JSONArray objects = JSON.parseArray(result.toString());
        List<SubjectFourQuestionTrunk> queList = new ArrayList<>();
        for (int i = 0; i < objects.size(); i++) {
            JSONArray jsonArray = objects.getJSONArray(i);
            SubjectFourQuestionTrunk sub = new SubjectFourQuestionTrunk();
            Object o = jsonArray.get(0);
//            System.out.println(o);
            sub.setId(Integer.valueOf((String)jsonArray.get(0)));
            sub.setType((String) jsonArray.get(1));
            sub.setQuestion((String) jsonArray.get(2));
            sub.setOptionOne((String) jsonArray.get(3));
            sub.setOptionTwo((String) jsonArray.get(4));
            sub.setOptionThree((String) jsonArray.get(5));
            sub.setOptionFour((String) jsonArray.get(6));
//            Integer.valueOf((String)jsonArray.get(7));
            sub.setAnswer((String)jsonArray.get(7));
            sub.setPicture((String) jsonArray.get(8));
            sub.setNoMeaningOne((String) jsonArray.get(9));
            sub.setDescription((String) jsonArray.get(10));
            sub.setNoMeaningTwo((String) jsonArray.get(11));
            sub.setNoMeaningThree((String) jsonArray.get(12));

            queList.add(sub);
        }
        for (SubjectFourQuestionTrunk subjectOneQuestion : queList) {
            boolean save = subjectFourTrunk.saveOrUpdate(subjectOneQuestion);

        }
//        boolean b = subjectOne.saveBatch(queList);
//        for (SubjectOneQuestion subjectOneQuestion : queList) {
//            subjectMapper.insert(subjectOneQuestion);
//        }

        System.err.println(queList);
    }

}
